package com.erp.myAdapter

import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.EditText
import com.erp.R
import com.erp.bean.YuliaoGiFreightBean
import com.erp.mclass.QuanXian
import com.erp.module.yuliaofreight.YuliaoFreightConteract
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.*
import kotlinx.android.synthetic.main.lv_yuliao_gi_peright.view.*

class YuliaoGiFrightAdapter(var yuliaoList: MutableList<YuliaoGiFreightBean>, var mcontext: Context, var KP: YuliaoFreightConteract.YuliaoGiFreightView) : BaseAdapter(){
    var sp: SharedPreferences = Sputil.sp("erp");
    private val minflater: LayoutInflater = LayoutInflater.from(mcontext)

    @SuppressLint("ViewHolder", "InflateParams")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var v : View=minflater.inflate(R.layout.lv_yuliao_gi_peright,null)
        val bean=yuliaoList[position]
        Myutil.settv(bean.code, v.tvCode)
        Myutil.settv(bean.freight_space_name, v.tvCangweiName)
        Myutil.settv(bean.name, v.tvName)
        Myutil.settv(bean.length, v.tvLenght)
        Myutil.settv(bean.width, v.tvWidth)
        Myutil.settv(bean.thickness, v.tvThickness)
        Myutil.settv(bean.color, v.tvColor)
        Myutil.settv(Myutil.getdata(bean.intime), v.tvRucangTime)
        if(!QuanXian.isEditYuliaoFreightSpaceGi){
            v.OutBt.visibility = View.GONE
            v.OutQuliao.visibility = View.GONE
        }else{
            v.OutBt.setOnClickListener {
                var dialog= AlertDialog.Builder(mcontext)
                dialog.setTitle("提示")
                dialog.setMessage("确定出仓？")
                dialog.setPositiveButton("确定") { dialog, _ -> KP.outYuliaoChucang(bean.yfsg_id) }
                dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                dialog.show()
            }
            v.OutQuliao.setOnClickListener {
                val dialog = AlertDialog.Builder(mcontext).create()
                dialog.setView(EditText(mcontext))
                dialog.show()
                dialog.setContentView(R.layout.dialog_etyuliaocangwei)
                dialog.CangweiEt.setLines(20)
                val window = dialog.window
                dialog.title.text = "取料"
                val p = window?.attributes // 获取对话框当前的参数值
                if (p != null) {
                    p.width = (sp.getInt("width", 0) * 0.9).toInt()
                } // 宽度设置为屏幕的0.65
                if (window != null) {
                    window.attributes = p
                }
                dialog.LeftBt.setOnClickListener {
                    var s=dialog.CangweiEt.text.toString()
                    if(s.trim()==""){
                        Myutil.MyTost("必须输入长度才能确定")
                    }else {
                        KP.fetch_yuliao(bean.yfsg_id, dialog.CangweiEt.text.toString())
                        dialog.dismiss()
                    }
                }
                dialog.RightBt.setOnClickListener {
                    dialog.dismiss()
                }
            }
        }
        return v
    }

    override fun getItem(position: Int): Any = yuliaoList[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getCount(): Int = yuliaoList.size

}